package com.example.test;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
public class ShowVocabulary extends Activity implements
        AdapterView.OnItemSelectedListener, android.view.View.OnClickListener {
	int width;
	TextView mTextContent;
	int mPosition;
	CustomGallery g;
	private List<VocabularyDto> mListVocal=null;
	final List<String> listUnit = new ArrayList<String>();
	protected List<String> selectedDevices = new ArrayList<String>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.showvocabulary);
        mTextContent = (TextView) findViewById(R.id.text_content);
        Display display = getWindowManager().getDefaultDisplay(); 
        width = display.getWidth();  // deprecated
        g = (CustomGallery) findViewById(R.id.gallery);
        new VocabularyTask(this).execute();
    }
 
    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    	switch (v.getId()) {
		case R.id.gallery:
			
			break;
		default:
			break;
		}
    	mPosition = position;
    	mTextContent.setText("");
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
 
    public class ImageAdapter extends BaseAdapter {
    	private List<VocabularyDto> listVoca=null;
        public ImageAdapter(Context c, List<VocabularyDto> list) {
            mContext = c;
            listVoca = list;
        }
 
        public int getCount() {
            return listVoca.size();
        }
 
        public Object getItem(int position) {
            return position;
        }
 
        public long getItemId(int position) {
            return position;
        }
 
        public View getView(int position, View convertView, ViewGroup parent) {
           LayoutInflater inflater = LayoutInflater.from(mContext);
           View view = inflater.inflate(R.layout.galeryitem, null);
           TextView textView = (TextView) view.findViewById(R.id.text_item);
           textView.setText(listVoca.get(position).jp_stran);
           RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(width, RelativeLayout.LayoutParams.WRAP_CONTENT);
           textView.setLayoutParams(param);
            return view;
        }
 
        private Context mContext;
 
    }
 
    private class VocabularyTask extends AsyncTask<String, String , Integer>{
		Context mContext;
		public VocabularyTask(Context context){
			mContext=context;
		}
		@Override
		protected Integer doInBackground(String... params) {
			VocabularyDao vocal=new VocabularyDao(mContext);
			mListVocal =vocal.getListAllVocabulary();
			for (int i = mListVocal.size()-1;i>=0 ; i--) {
				String lession= Integer.toString(mListVocal.get(i).lession);
				if (!listUnit.contains(lession)) {
					listUnit.add(lession);
				}
			}
			return 0;
		}
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			if(mListVocal!=null && mListVocal.size()>0){
				setUnitTest();
			}
		}
	}
    private void setUnitTest() {
    	((Button)findViewById(R.id.btn_setlect_unit)).setOnClickListener(this);  
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_setlect_unit:
			showSelectDevicesDialog();
			break;

		default:
			break;
		}
	}
	protected void showSelectDevicesDialog() {
		boolean[] checkedDevices = new boolean[listUnit.size()];
		int count = listUnit.size();

		for(int i = 0; i < count; i++){
			checkedDevices[i] = selectedDevices.contains(listUnit.get(i));
		}

		DialogInterface.OnMultiChoiceClickListener coloursDialogListener = new DialogInterface.OnMultiChoiceClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				if(isChecked)
					selectedDevices.add(listUnit.get(which));
				else
					selectedDevices.remove(listUnit.get(which));

			}
		};
		AlertDialog.Builder builder = new AlertDialog.Builder(ShowVocabulary.this);
		builder.setTitle("Select Unit");
		builder.setMultiChoiceItems(listUnit.toArray(new CharSequence[listUnit.size()]), checkedDevices, coloursDialogListener);
		builder.setNeutralButton("OK", new DialogButtonClickHandler());
		AlertDialog dialog = builder.create();
		ListView listViewCheck = ((AlertDialog) dialog).getListView();
		listViewCheck.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		dialog.show();
	}
	public class DialogButtonClickHandler implements DialogInterface.OnClickListener
	{
		public void onClick( DialogInterface dialog, int clicked )
		{
			switch( clicked )
			{
				case DialogInterface.BUTTON_NEUTRAL:
					List<VocabularyDto> listVocabularySelected = new ArrayList<VocabularyDto>();
					if (selectedDevices.size() != 0) {
						for (int i = mListVocal.size()-1;i>=0 ; i--) {
							if (selectedDevices.contains(Integer.toString(mListVocal.get(i).lession))) {
								listVocabularySelected.add(mListVocal.get(i));
							}
						}
			                g.setAdapter(new ImageAdapter(ShowVocabulary.this, listVocabularySelected));
			  		        g.setOnItemSelectedListener(ShowVocabulary.this);
			  		        g.setOnItemClickListener(new OnItemClickListener() {
			  					@Override
			  					public void onItemClick(AdapterView<?> arg0, View arg1,
			  							int arg2, long arg3) {
			  						mTextContent.setText(mListVocal.get(mPosition).vn_stran);
			  					}
			  				});
					}else{}
					break;
				case DialogInterface.BUTTON_POSITIVE:
					break;
			}
		}
	}
}

